#include #include using namespace std; void displayNumberWithCommas(int number); int digitCount(int number) { int result = 1; while(number > 9) { result++; number /= 10; } return result; } void main() { int dollars; cin >> dollars; //12,345 //displayNumberWithCommas(dollars); cout << digitCount(dollars) << endl; } //assumes the number is a value from 0 - 999 void dispayNumberWithPadding(int number) { //dispays 0 or 00 or 000 or nothing //when the number is 0 dispay 000 //when number 1-9 dispay 00 //when number 10-99 dispay 0 //otherwise dispay noting if(number == 0) { cout << "000"; } else if(number > 0 && number < 10) { cout << "00"; cout << number; } else if(number >= 10 && number < 100 ) { cout << "0"; cout << number; } else //100 - 999 { cout << number; } } void displayNumberWithCommas(int number) { int billions = number/1000000000; number = number - billions * 1000000000; int millions = number/1000000; number = number - millions * 1000000; int thousands = number/1000; number = number - thousands * 1000; int hundreds = number; bool mustPad = false; //12345 0,0,12,345 if(billions > 0) { cout << billions << ","; mustPad = true; } if(mustPad) { dispayNumberWithPadding(millions); cout << ","; } else if(millions > 0) { cout << millions << ","; mustPad = true; } if(mustPad) { dispayNumberWithPadding(thousands); cout << ","; } else if(thousands > 0) { cout << thousands << ","; mustPad = true; } if(mustPad) { dispayNumberWithPadding(hundreds); } else { cout << hundreds << endl; } }